<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

class Course extends Base
{

    /**
     * getInfo
     * @return Json
     * @throws BusinessException
     */
    public function getInfo(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        app(\app\api\logic\Course::class)->getInfo($param);

        // 返回
        return $this->response();
    }

    /**
     * 我的课程
     * @return Json
     * @throws
     */
    public function myCourse(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 查询数据
        $response = (new \app\api\logic\Course())->myCourse($param);

        // 返回数据
        return $this->response($response);
    }

    /**
     * 课程销售说明
     * @return Json
     * @throws
     */
    public function saleInfo(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 查询数据
        $response = (new \app\api\logic\Course())->saleInfo($param);

        // 返回数据
        return $this->response($response);
    }

    /**
     * 搜索
     * @throws BusinessException
     */
    public function search(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'page' => 'require',
            'keyword' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'page.require' => '页码page不得为空',
            'keyword.require' => '关键字keyword不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交查询
        $response = app(\app\api\logic\Course::class)->search($param);

        // 返回
        return $this->response($response);
    }

    /**
     * 获取用户课程学习统计数据
     * @return Json
     * @throws BusinessException
     */
    public function getUserStats(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'course_id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'course_id.require' => '课程ID不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 获取用户统计数据
        $response = app(\app\api\logic\Course::class)->getUserStats($param);

        // 返回
        return $this->response($response);
    }

    public function category()
    {
        $categoryList = [
            ''
        ];
    }
}
